package engine

import (
	"gxgeek.com/gxgeek/learngo/crawler/config"
)

type Request struct {
	Url    string
	Parser Parser
}
type ParseResult struct {
	Requests [] Request
	Items    []Item
}

type Parser interface {
	Parse(contents [] byte, url string) ParseResult
	Serialize() (name string, args interface{})
}
type ParserFunc func(contents [] byte, url string) ParseResult
type Item struct {
	Url     string
	Id      string
	Type    string
	Payload interface{}
}

type NilParser struct{}

func (NilParser) Parse(_ [] byte, _ string) ParseResult {
	return ParseResult{}
}

func (NilParser) Serialize() (name string, args interface{}) {
	return config.NilParser, nil
}
func NilParserFunc([] byte) ParseResult {
	return ParseResult{}
}

type FuncParser struct {
	 parser ParserFunc
	name   string
}

func (f *FuncParser) Parse(contents [] byte, url string) ParseResult {
	return f.parser(contents,url)
}

func (f *FuncParser) Serialize() (name string, args interface{}) {
	return f.name,nil
}

func NewFuncParser(p ParserFunc, funName string) *FuncParser {
	return &FuncParser{parser: p, name: funName}
}
